﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Electric;
using YArchitech.LIB;
using YArchitech.Revit;
using YArchitech.Revit.Electric.Common;

namespace YArchitech.MEP.Electric.CableLay
{
	public partial class EquipmentCodeCheckForm : System.Windows.Forms.Form
	{
		public EquipmentCodeCheckForm(UIDocument uiDocument)
		{
			this.InitializeComponent();
			this.uiDoc = uiDocument;
			this.doc = uiDocument.Document;
			this.datas = new Dictionary<string, List<ElementId>>();
		}

		private void EquipmentCodeCheckForm_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			this.InitButtonImage();
			this.ResetFormSize(false);
			this.UpdateDataGridView();
		}

		private void InitButtonImage()
		{
			this.buttonRefresh.Image = YArchitech.Electric.Resources.GetImage("Refresh16");
			this.buttonRefresh.TextImageRelation = TextImageRelation.ImageBeforeText;
			this.buttonZoom.Image = YArchitech.Electric.Resources.GetImage("Right");
			this.buttonSee.Image = YArchitech.Electric.Resources.GetImage("ViewSee16");
			this.buttonSee.TextImageRelation = TextImageRelation.ImageBeforeText;
		}

		private void ResetFormSize(bool isExpand)
		{
			this.IsExpand = isExpand;
			if (this.IsExpand)
			{
				base.Width = 630;
				this.MinimumSize = new Size(630, this.MinimumSize.Height);
				this.MaximumSize = new Size(630, this.MaximumSize.Height);
				this.buttonZoom.Image = YArchitech.Electric.Resources.GetImage("Left");
				return;
			}
			base.Width = 258;
			this.MinimumSize = new Size(258, this.MinimumSize.Height);
			this.MaximumSize = new Size(258, this.MaximumSize.Height);
			this.buttonZoom.Image = YArchitech.Electric.Resources.GetImage("Right");
		}

		private void GetSameCodeEquipments()
		{
			this.datas.Clear();
			ICollection<ElementId> collection = new FilteredElementCollector(this.doc).OfClass(typeof(FamilyInstance)).ToElementIds();
			if (collection == null || collection.Count == 0)
			{
				return;
			}
			collection = this.ElectricalFilter(collection);
			foreach (ElementId elementId in collection)
			{
				Element element = this.doc.GetElement(elementId);
				if (element != null)
				{
					string text = string.Empty;
					Parameter parameter = element.GetParameter("编号");
					if (parameter != null)
					{
						text = parameter.AsString();
						if (string.IsNullOrEmpty(text))
						{
							text = "未编号";
						}
						if (this.datas.ContainsKey(text))
						{
							this.datas[text].Add(elementId);
						}
						else
						{
							List<ElementId> list = new List<ElementId>();
							list.Add(elementId);
							this.datas.Add(text, list);
						}
					}
				}
			}
		}

		private ICollection<ElementId> ElectricalFilter(ICollection<ElementId> entIdSet)
		{
			if (entIdSet == null || entIdSet.Count == 0)
			{
				return entIdSet;
			}
			List<ElementId> list = new List<ElementId>();
			foreach (ElementId elementId in entIdSet)
			{
				Element element = this.doc.GetElement(elementId);
				if (element != null)
				{
					FamilyInstance familyInstance = element as FamilyInstance;
					if (familyInstance != null && familyInstance.MEPModel != null && familyInstance.MEPModel.ConnectorManager != null)
					{
						ConnectorSet connectors = familyInstance.MEPModel.ConnectorManager.Connectors;
						if (connectors != null && connectors.Size > 0)
						{
							ConnectorSetIterator connectorSetIterator = connectors.ForwardIterator();
							while (connectorSetIterator.MoveNext())
							{
								if ((connectorSetIterator.Current as Connector).Domain == (Autodesk.Revit.DB.Domain)2)
								{
									list.Add(elementId);
									break;
								}
							}
						}
					}
				}
			}
			return list;
		}

		private void UpdateDataGridView()
		{
			this.GetSameCodeEquipments();
			this.dataGridView.Rows.Clear();
			if (this.datas.Count == 0)
			{
				MessageBox.Show("未发现编号重复的设备。", "盈建科提示", MessageBoxButtons.OK);
				base.Close();
				return;
			}
			foreach (KeyValuePair<string, List<ElementId>> keyValuePair in this.datas)
			{
				if (keyValuePair.Value.Count > 1)
				{
					DataGridViewRow dataGridViewRow = new DataGridViewRow();
					int index = this.dataGridView.Rows.Add(dataGridViewRow);
					dataGridViewRow = this.dataGridView.Rows[index];
					dataGridViewRow.Cells[0].Value = keyValuePair.Key;
					dataGridViewRow.Cells[1].Value = keyValuePair.Value.Count;
				}
			}
			if (this.dataGridView.Rows.Count > 0)
			{
				this.dataGridView.ClearSelection();
				this.dataGridView.Rows[0].Selected = true;
				return;
			}
			MessageBox.Show("未发现编号重复的设备。", "盈建科提示", MessageBoxButtons.OK);
			base.Close();
		}

		private void dataGridView_SelectionChanged(object sender, EventArgs e)
		{
			if (this.dataGridView.SelectedRows.Count <= 0)
			{
				return;
			}
			DataGridViewRow dataGridViewRow = this.dataGridView.SelectedRows[0];
			if (dataGridViewRow == null)
			{
				return;
			}
			string text = (string)dataGridViewRow.Cells[0].Value;
			if (string.IsNullOrEmpty(text) || !this.datas.ContainsKey(text))
			{
				return;
			}
			this.dataGridViewEx.Rows.Clear();
			foreach (ElementId elementId in this.datas[text])
			{
				Element element = this.doc.GetElement(elementId);
				if (element != null && element.IsValidObject)
				{
					FamilyInstance familyInstance = element as FamilyInstance;
					if (familyInstance != null)
					{
						string name = familyInstance.Symbol.Family.Name;
						ElementId levelId = element.LevelId;
						Level level = this.doc.GetElement(levelId) as Level;
						Parameter parameter = element.GetParameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM);
						if (level != null && parameter != null)
						{
                            int num = (int)Convert.ToInt16(UnitConvert.CovertFromAPI((DisplayUnitType)2, parameter.AsDouble()));
							string value = string.Empty;
							Parameter parameter2 = element.GetParameter("备注");
							if (parameter2 != null)
							{
								value = parameter2.AsString();
							}
							DataGridViewRow dataGridViewRow2 = new DataGridViewRow();
							int index = this.dataGridViewEx.Rows.Add(dataGridViewRow2);
							dataGridViewRow2 = this.dataGridViewEx.Rows[index];
							dataGridViewRow2.Tag = elementId;
							dataGridViewRow2.Cells[0].Value = name;
							dataGridViewRow2.Cells[1].Value = ((level != null) ? level.Name : string.Empty);
							dataGridViewRow2.Cells[2].Value = num;
							dataGridViewRow2.Cells[3].Value = value;
						}
					}
				}
			}
		}

		private void buttonZoom_Click(object sender, EventArgs e)
		{
			this.ResetFormSize(!this.IsExpand);
		}

		private void buttonRefresh_Click(object sender, EventArgs e)
		{
			this.UpdateDataGridView();
		}

		private void buttonSee_Click(object sender, EventArgs e)
		{
			if (this.dataGridViewEx.SelectedRows.Count <= 0)
			{
				return;
			}
			DataGridViewRow dataGridViewRow = this.dataGridViewEx.SelectedRows[0];
			if (dataGridViewRow == null)
			{
				return;
			}
			ElementId elementId = dataGridViewRow.Tag as ElementId;
			if (null == elementId)
			{
				return;
			}
			Element element = this.doc.GetElement(elementId);
			if (element == null || !element.IsValidObject)
			{
				return;
			}
			if (ViewUtils.ElementIsShowInView(this.doc, elementId, this.uiDoc.ActiveView.Id))
			{
				CableLayRevitCommon.HighlightElement(this.uiDoc, element);
				return;
			}
			List<ElementId> viewsOpened = ViewUtils.GetViewsOpened(this.uiDoc, (CmdViewType)19);
			foreach (ElementId viewId in viewsOpened)
			{
				if (ViewUtils.ElementIsShowInView(this.doc, elementId, viewId))
				{
					CableLayRevitCommon.HighlightElement(this.uiDoc, element);
					return;
				}
			}
			foreach (ElementId elementId2 in ViewUtils.GetViews(this.doc, (CmdViewType)3))
			{
				if (!viewsOpened.Contains(elementId2) && ViewUtils.ElementIsShowInView(this.doc, elementId, elementId2))
				{
                    Autodesk.Revit.DB.View view = this.doc.GetElement(elementId2) as Autodesk.Revit.DB.View;
					if (view != null)
					{
						this.uiDoc.ActiveView = view;
						CableLayRevitCommon.HighlightElement(this.uiDoc, element);
						return;
					}
				}
			}
			foreach (ElementId elementId3 in ViewUtils.GetViews(this.doc, CmdViewType.ThreeD))
			{
				if (!viewsOpened.Contains(elementId3) && ViewUtils.ElementIsShowInView(this.doc, elementId, elementId3))
				{
                    Autodesk.Revit.DB.View view2 = this.doc.GetElement(elementId3) as Autodesk.Revit.DB.View;
					if (view2 != null)
					{
						this.uiDoc.ActiveView = view2;
						CableLayRevitCommon.HighlightElement(this.uiDoc, element);
						break;
					}
				}
			}
		}

		private void dataGridViewEx_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
		{
			this.buttonSee_Click(null, null);
		}

		private UIDocument uiDoc;

		private Document doc;

		private bool IsExpand;

		private Dictionary<string, List<ElementId>> datas;
	}
}
